import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/pay_free.dart';
import 'package:app/src/http/bean/wroks/pay_notice_bean.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/receipt_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../../http/bean/works_2023/transport_bean.dart';
import '../../../../app_ui.dart';

///运输合同审核：运输线路车辆信息
class UiTransportAuditForm {
  static Widget build(BuildContext context, LogisticsContractBean? data,
      {String? title}) {
    if (StringUtils.isEmpty(data)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).Transportline; //运输线路车辆信息
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(
      context,
      data,
    ));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(
    BuildContext context,
    LogisticsContractBean? data,
  ) {
    var datas = data?.logisticsContractList;
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        List<TableRow> children = [];
        //==========================================================================标题
        children.add(TableRow(
            //第一行，添加背景色
            decoration:
                BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
            children: _titles(context)));
        if (datas != null && datas.length > 0) {
          var postion = 1;
          datas.forEach((element) {
            //======================================================================内容
            var cells = _contents(context, postion, data, element, setState);
            postion++;
            children.add(TableRow(
                children: cells,
                decoration: BoxDecoration(
                    color: AppColors.getColorWithTableRow(postion))));
          });
        }
        var table = Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: {
            0: FixedColumnWidth(50),
            1: FixedColumnWidth(150),
            // 6: FixedColumnWidth(180),
          },
          border: AppStyle.getTableBorder(),
          textDirection: TextDirection.ltr,
          children: children,
        );
        var width = 1800.0;
        if (width < px.screenWidth()) {
          width = px.screenWidth();
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(),
          child: Container(
            width: width,
            child: table,
          ),
        );
      },
    );
  }

  ///标题
  static List<Widget> _titles(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    children.add(_addItem(AppStrings.of(context).MaterialType)); //物料类型
    children
        .add(_addItem(AppStrings.of(context).StartAddress, isStart: true)); //起点
    children
        .add(_addItem(AppStrings.of(context).EndAddress, isStart: true)); //终点
    children.add(
        _addItem(AppStrings.of(context).ValuationType, isStart: true)); //计价方式
    children.add(_addItem(AppStrings.of(context).Mileage, isStart: true)); //里程数
    children.add(_addItem(AppStrings.of(context).Price2, isStart: true)); //单价
    children.add(_addItem(AppStrings.of(context).SuperelevationSpec,
        isStart: true)); //超高约定
    children.add(_addItem(AppStrings.of(context).Superelevationfreight,
        isStart: true)); //超高单价
    return children;
  }

  ///内容
  static List<Widget> _contents(
      BuildContext context,
      int postion,
      LogisticsContractBean? data,
      logisticsContractListBean element,
      StateSetter setState) {
    List<Widget> children = [];
    children.add(_addItem((postion).toString())); //序号
    children.add(_addItem(data?.getMaterialTypeName(context))); //物料类型
    children.add(_addItem(element.StartAddress)); //起点
    children.add(_addItem(element.EndAddress)); //终点
    children.add(_addItem(element.ValuationType)); //计价方式
    children.add(_addItem(
        "${element.Mileage}${AppStrings.of(context).kilometre}")); //里程数
    children.add(_addItem(element.Price)); //单价
    children.add(_addItem(
        "${AppStrings.of(context).Higherthan}${element.SuperelevationSpec}mm")); //超高约定
    children.add(_addItem(
        "${AppStrings.of(context).increase}${element.Superelevationfreight}${AppStrings.of(context).yuan}")); //超高单价
    return children;
  }

  static Widget _addItem(String? text,
      {bool isBlue = false, bool isStart = false, GestureTapCallback? onTap}) {
    Widget child = Center(child: _text(text, isBlue: isBlue, isStart: isStart));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  //文本
  static Widget _text(String? text,
      {bool isStart = false, bool isBlue = false}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    Widget child;
    if (isStart) {
      var textKey = Text(
        text ?? "",
        style: style,
      );
      var value = "*";
      var startText = Text(
        value,
        softWrap: true,
        style: AppStyle.textStyleWithCardRed(),
      );
      child = SizedBox(
        child: Row(
          children: [textKey, startText],
          mainAxisAlignment: MainAxisAlignment.center,
        ),
        width: 120,
      );
    } else {
      child = Text(
        text ?? "",
        style: style,
      );
    }
    return Center(
      child: ConstrainedBox(
        constraints: BoxConstraints(
          minHeight: 36.0, //最小高度
        ),
        child: Center(
          child: child,
        ),
      ),
    );
  }
}
